#include "StdAfx.h"
#include <math.h>
#include "UnrepeatingNumbers.h"

UnrepeatingNumbers::UnrepeatingNumbers(void)
{
}

UnrepeatingNumbers::~UnrepeatingNumbers(void)
{
}


long UnrepeatingNumbers::next(long A)
{
	if (A < 0 || A > pow(10.0, 17))
		return -1;

	int adj1, adj2;
	// Master number
	long master = A;
	bool hasRepeating = false;
	while (true) {
		A = master;
		A++;
		master = A;

		hasRepeating = false;
		// Check A 
		while (true) {
			adj1 = A % 10; 
			A = A / 10;
			if (A <  10) {
				// A is lower than 10
				adj2 = A;
			} else {
				adj2 = A % 10;
			}
			if (adj1 == adj2) {
				// A is repeating number
				hasRepeating = true;
				break;
			} else if (adj2 == A) {
				break;
			}
		}
		if (!hasRepeating)
			break;
	}
	return master;
}

long UnrepeatingNumbers::getNext(long A)
{
	A++;

	bool done = false;
	do {
		// calculate the exponent
		long expo = 1;
		while (expo * 10 < A)
			expo *= 10;
		
		done = true;
		while(expo > 1) {
			long dExpo = expo / 10;
			if ((A / expo) % 10 == (A / dExpo) % 10) {
				A /= dExpo;
				A++;
				A *= dExpo;
				done = false;
			}
			expo = dExpo;
		}
	} while (!done);

	return A;
}